<template>
  <el-card>
    <el-form inline>
      <el-form-item>
        <el-input placeholder="角色名称" v-model="roleName" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="Search">查询</el-button>
        <el-button>清空</el-button>
      </el-form-item>
    </el-form>

    <el-button type="primary" @click="ShowAddRole">添加</el-button>
    <el-button type="danger">批量删除</el-button>

    <el-table :data="roleList" border class="role-table" v-loading="loading">
      <el-table-column type="selection" width="60" />
      <el-table-column type="index" label="序号" width="60" align="center" />
      <el-table-column label="角色名称" prop="roleName" />
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-button
            size="small"
            type="info"
            :icon="Setting"
            @click="goRoleAuth(row)"
            v-permission="'btn.Role.assgin'"
          />
          <el-button size="small" type="primary" :icon="Edit"  v-permission="'btn.Role.update'"/>
          <el-button size="small" type="danger" :icon="Delete" v-permission="'btn.Role.remove'"/>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-model:currentPage="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[5, 10, 15, 20]"
      layout="prev, pager, next, jumper, ->, sizes, total"
      :total="total"
      @size-change="getRoleList"
      @current-change="getRoleList"
    />
  </el-card>
</template>

<script lang="ts">
export default {
  name: "XRole",
};
</script>

<script lang="ts" setup>
import { ref, onMounted } from "vue";
import { Search, Setting, Edit, Delete } from "@element-plus/icons-vue";
import { getRoleListApi, addRoleApi } from "@/api/acl/role";
import type { RoleList, RoleItem } from "@/api/acl/model/userModel";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";
const router = useRouter();

/*********************************************分页查询角色列表**********************/
const currentPage = ref(1);
const pageSize = ref(5);
const total = ref(0);
const loading = ref(false);
const roleName = ref("");
const roleList = ref<RoleList>([]);
const getRoleList = async () => {
  const res = await getRoleListApi({
    page: currentPage.value,
    limit: pageSize.value,
    roleName: roleName.value,
  });
  roleList.value = res.items;
  total.value = res.total;
  ElMessage.success("获取角色列表成功");
};
onMounted(getRoleList);

/******************************************添加角色的功能*********************************/
const ShowAddRole = () => {
  ElMessageBox.prompt("请输入新名称", "添加角色", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    inputPattern: /\S/,
    inputErrorMessage: "输入的内容不能为空",
  })
    .then(async ({ value }) => {
      await addRoleApi(value);
      ElMessage({
        type: "success",
        message: `添加角色成功`,
      });
      getRoleList();
    })
    .catch(() => {
      ElMessage({
        type: "warning",
        message: "取消添加",
      });
    });
};

/********************************************跳转路由到角色菜单权限列表*********************************** */
const goRoleAuth = (row: RoleItem) => {
  router.push({
    name: "RoleAuth",
    query: {
      id: row.id,
      roleName: row.roleName,
    },
  });
};
</script>

<style scoped>
.role-table {
  margin: 20px 0;
}
</style>
